package com.demo.consumer.controller.nsf;

import com.demo.consumer.controller.apm.ApmDeomController;
import com.demo.consumer.service.IStockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("nsf")
public class NsfDemoController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    IStockService stockService;

    @Value("${project.name}")
    String name ;

    @Value("${project.value}")
    String value ;

    @Value("${stock_provider_url}")
    String stockProviderUrl;

    private static final Logger log = LoggerFactory.getLogger(ApmDeomController.class);

//    String url = "http://127.0.0.1:9001/instance/postinfo";

    @GetMapping("failover")
    public void failover() throws InterruptedException {
        log.info("/nsf/failover call failover api！");
        throw new RuntimeException("failover 报个错");
    }

    @GetMapping("badgateway")
    public ResponseEntity<Map<String,Object>> badgateway() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bad gateway", "502");
        log.info("/nsf/badgateway call badgateway api!");
        return new ResponseEntity<Map<String, Object>>(map, HttpStatus.BAD_GATEWAY);
    }

    @GetMapping("timelay")
    public String timelay() throws InterruptedException {
        Thread.sleep(2000);
        log.info("/nsf/timelay call timelay 2000ms!");
        System.out.println("demo-consumer/nsf timelay is getd");
        return "timelay 2000ms";
    }

    @GetMapping("load")
    public String nsfload(@RequestParam(value="content",defaultValue = "aaa",required = false) String content) {
        String url = stockProviderUrl+"/instance/postinfo";
        //设置get请求参数
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("content", content);
        //设置cookie参数
        List<String> cookies = new ArrayList<>();
        cookies.add("token=928d7aed1aa04f6caaa1b379040b4b0b");
        //设置请求头参数
        HttpHeaders  requestHeaders = new HttpHeaders();
        requestHeaders.add("x-name","hedu");
        requestHeaders.put(HttpHeaders.COOKIE,cookies);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,requestHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url,httpEntity,String.class);
        log.info("/nsf/load {}",response);
        return response.getBody();
    }

    @GetMapping("fault")
    public String fault(String action){
/*
        MultiValueMap<String,Object> paramMap = new LinkedMultiValueMap();
        paramMap.add("action",action);
        System.out.println(action);
        ResponseEntity<String> response =restTemplate.postForEntity(stockProviderUrl+"/nsf/fault", paramMap,String.class);
        return response.getBody();
 */
        String url = stockProviderUrl+"/nsf/fault";
        String response =  restTemplate.getForObject(url, String.class);
        log.info("/nsf/fault call provider:/nsf/provider {}", response);
        return  response;
    }

    @GetMapping("apolloconfig")
    public String apolloconfig(){
        return "project.name当前的值为---->"+name + '\n'+ "project.value当前的值为---->"+value;
    }

    @GetMapping("/send/provider")
    @ResponseBody
    public String sendProvider(HttpServletRequest request,
                                     @RequestParam(name = "content", defaultValue = "test", required = false) String content) {
        String result = stockService.sendProvider(content);
        return result;

    }



}
